﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.Data.SqlClient;
using System.Windows.Forms;
using System.IO;
using System.Xml;

namespace Medical_Store_Billing.Component
{
    partial class Dataservice : DataTable
    {
        private static SqlConnection m_Connection;
        public static String m_ConnectString = "Data Source=";
        private SqlCommand m_Command;
        private SqlDataAdapter m_DataAdapter;

        #region ReadConfig
        public static bool ReadConfig()
            {
              string fileName = "config.Xml";
              FileStream fs = new FileStream(fileName, FileMode.Open);
              XmlTextReader xtr = new XmlTextReader(fs);
              try
              {
                  xtr.Read();
                  xtr.ReadStartElement("Ketnoi");
                  xtr.ReadStartElement("server");
                  m_ConnectString = m_ConnectString + xtr.ReadString() + "; Initial Catalog=";
                  xtr.ReadEndElement();
                  xtr.ReadStartElement("database");
                  m_ConnectString = m_ConnectString + xtr.ReadString() + "; Uid=";
                  xtr.ReadEndElement();
                  xtr.ReadStartElement("user");
                  m_ConnectString = m_ConnectString + xtr.ReadString() + "; pwd=";
                  xtr.ReadEndElement();
                  xtr.ReadStartElement("pass");
                  m_ConnectString = m_ConnectString + xtr.ReadString();
                  xtr.ReadEndElement();
                  xtr.ReadEndElement();
                  xtr.Close();
                  fs.Close();
                  
                  return true;
              }
              catch { return false; }
            }
        #endregion

        #region WriteConfig
        public static void WriteConfig(String server, String database, String user, String pass)
        {
            
            XmlTextWriter writer = new XmlTextWriter ("config.Xml",null);
            
            writer.Formatting = Formatting.Indented;
        
            
            writer.WriteStartDocument();
            writer.WriteStartElement("Ketnoi");

            writer.WriteElementString("server", server);
            writer.WriteElementString("database", database);
            writer.WriteElementString("user", user);
            writer.WriteElementString("pass", pass);
            writer.WriteEndElement();
            writer.WriteEndDocument();
            writer.Flush();
            writer.Close();
        }
        #endregion

        #region OpenConection
        public static bool OpenConnection()
            {
                ReadConfig();
                try
                {
                    if (m_Connection == null)
                        m_Connection = new SqlConnection(m_ConnectString);
                    if (m_Connection.State == ConnectionState.Closed)
                        m_Connection.Open();
                    return true;
                }
                catch
                {
                    m_Connection.Close();
                    return false;
                }
            }
        #endregion

        #region CloseConnection
        public void CloseConnection()
        {
            m_Connection.Close();
        }
        #endregion

        #region LoadTable
        public void Load(SqlCommand m_sql)
        {
            try
            {
                m_Command = m_sql;
                m_Command.Connection = m_Connection;
                m_DataAdapter = new SqlDataAdapter();
                m_DataAdapter.SelectCommand = m_Command;
                this.Clear();
                m_DataAdapter.Fill(this);
            }
            catch
            {
                MessageBox.Show("Không thể thực thi câu lệnh SQL này!", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
        #endregion

        #region UpdateTable
        public int ExecuteNonQuery(SqlCommand m_sql)
        {
            int result = 0;
            SqlTransaction m_sqltran = null;
            try
            {
                m_sqltran = m_Connection.BeginTransaction();
                m_sql.Connection = m_Connection;
                m_sql.Transaction = m_sqltran;
                result = m_sql.ExecuteNonQuery();

                this.AcceptChanges();
                m_sqltran.Commit();
            }
            catch
            {
                if (m_sqltran != null)
                    m_sqltran.Rollback();
                throw;
            }
            return result;
        }
        #endregion
    
    }
}
